import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control
import { removeToken, getRole, removeRole } from '@/utils/auth'
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
// import { mockXHR } from "../mock";
// if (process.env.NODE_ENV === "production") {
//   mockXHR();
// }

// set ElementUI lang to CN
Vue.use(ElementUI, { locale })

Vue.config.productionTip = false

if (getRole()) {
  store.dispatch('permission/generateRoutes', [getRole()]).then(res => {
    const arr = store.getters.addRoutes
    if (JSON.parse(localStorage.getItem('vip_rule')) === 0) {
      for (let i = 0; i < arr.length; i++) {
        const obj = arr[i]
        if (obj.children) {
          const children = obj.children
          for (let j = children.length - 1; j >= 0; j--) {
            if (children[j].path === 'vip-goods' || children[j].path === 'vip-order' || children[j].path === 'vip-poster' || children[j].path === 'vip-teacher' || children[j].path === 'college-mng') {
              children.splice(j, 1)
            }
          }
        }
      }
      localStorage.setItem('isShowVipCardSetting', false)
    } else if (JSON.parse(localStorage.getItem('vip_rule')) === 1) {
      localStorage.setItem('isShowVipCardSetting', true)
      for (let i = 0; i < arr.length; i++) {
        const obj = arr[i]
        if (obj.children) {
          const children = obj.children
          for (let j = 0; j < children.length; j++) {
            if (children[j].path === 'commission') {
              children.splice(j, 1)
            }
          }
        }
      }
    }
    router.addRoutes(arr)
    router.options.routes = router.options.routes.concat(arr)
  })
} else {
  removeToken()
  router.replace('/login')
}

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
